import { HttpStatus } from '@nestjs/common';
import moment from 'moment';
import { Model, UpdateQuery } from 'mongoose';
import { HttpErrorException } from 'src/common/exceptions/http-error.exception';
import { successUpdate } from 'src/common/responses';
import { ResponseBodyBaseType } from 'src/typings';

/**
 * 通过Id更新数据
 * @param model MongoDBModel
 * @param _id 要修改数据的ID
 * @param updateQuery 要更新的数据
 * @returns 更新结果
 */
export const updateOneById = async <M, U>(
  model: Model<M>,
  _id: string,
  updateQuery: UpdateQuery<U>,
) => {
  try {
    await model.findByIdAndUpdate(_id, {
      updateTime: new Date(),
      ...updateQuery,
    });
    return {
      message: '更新成功',
      success: true,
      statusCode: HttpStatus.OK
    };
  } catch (error: any) {
    throw new HttpErrorException(error.message);
  }
};
